package com.amway.qy.message.resp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.amway.qy.core.constants.RespMsgType;
import com.amway.qy.message.resp.section.RespArticleSection;

public class RespNewsMessage extends RespBaseMessage {
	
	//图文条数，默认第一条为大图。图文数不能超过10，否则将会无响应
	private final static int MAX_ITEMS=10;

	//图文条数，默认第一条为大图。图文数不能超过10，否则将会无响应
	protected Integer ArticleCount;
	
	protected List<RespArticleSection> Articles ;
	
	/*
	 * 考虑到与微信的交互，有相关的特定字段约束（e.g 必有项），为防止上层业务用户调用时，发生错漏。需要在创建相应的实例时，就必须提供必填项。
	 */
	public RespNewsMessage(String toUserName, String fromUserName,List<RespArticleSection> articles) {
		super(toUserName, fromUserName);
		if(articles==null || articles.isEmpty()){
			throw new RuntimeException("articles cannot be Null or Empty.");
		}
		
		if(articles.size()>MAX_ITEMS){
			throw new IllegalStateException("The Size of News cannot exceed 10 items.");
		}
		
		this.Articles = new ArrayList<RespArticleSection>(articles);
		this.ArticleCount = Articles.size();
		this.setMsgType(RespMsgType.RESP_MESSAGE_TYPE_NEWS);
		
		
	}
	
	public void add(RespArticleSection item){
		this.Articles.add(item);
		this.ArticleCount = Articles.size();
	}
	
	public void remove(RespArticleSection item){
		this.Articles.remove(item);
		this.ArticleCount = Articles.size();	
	}
	
	public Integer getArticleCount() {
		return ArticleCount;
	}

	public void setArticles(List<RespArticleSection> articles) {
		Articles = articles;
		this.ArticleCount = Articles.size();
	}
	
	/*
	 * 这里返回的List，是个只读的list，无法更新。
	 */
	public List<RespArticleSection> getArticles() {
		return Collections.unmodifiableList(Articles);
	}


	@Override
	public String toString() {
		return "RespNewsMessage [ArticleCount=" + ArticleCount + ", Articles=" + Articles + ", ToUserName=" + ToUserName
				+ ", FromUserName=" + FromUserName + ", CreateTime=" + CreateTime + ", MsgType=" + MsgType + "]";
	}
	
	
}
